﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace InnovTech.Localization
{
    public abstract class FileLocalizerBase 
    {

       
        private List<string> _allowFileTypes;
        public IEnumerable<string> AllowFileTypes
        {
            get
            {
                if (_allowFileTypes == null)
                    _allowFileTypes = new List<string>();
                return _allowFileTypes;
            }
        }


        private string _fileFullPath;
        public string FileFullPath
        {
            get
            {
                return _fileFullPath;
            }
            set
            {
                _fileFullPath = value;
            }
        }

        public void Localize()
        {
            EnsureFileExist(FileFullPath);
            ExtractResourcesEventArgs args = new ExtractResourcesEventArgs() { FileName = FileFullPath };
            Localizing(this, args);

            if (args.Cancel) return;

            LocalizeFileCore(FileFullPath);

            Localized(this, args);
        }

        public void LocalizeFile(string file)
        {
            FileFullPath = file;
            Localize();
        }

        protected void EnsureFileExist(string file)
        {
            if (!File.Exists(file))
                throw new FileNotFoundException(string.Format("The file {0} to localize does not exist", file));
        }
        


        public void AddAllowedFileType(string type)
        {
            _allowFileTypes.Add(type);
        }

        public void AddAllowedFileTypes(IEnumerable<string> types)
        {
            _allowFileTypes.AddRange(types);
        }

        public event EventHandler<ExtractResourcesEventArgs> Localizing = delegate { };

        public event EventHandler<ExtractResourcesEventArgs> Localized = delegate { };

        protected abstract void LocalizeFileCore(string file);


        
    }
}
